package cn.wolfcode.trip.base.service.impl;

import cn.wolfcode.trip.base.domain.StrategyContent;
import cn.wolfcode.trip.base.domain.StrategyDetail;
import cn.wolfcode.trip.base.domain.Strategycatalog;
import cn.wolfcode.trip.base.mapper.StrategyContentMapper;
import cn.wolfcode.trip.base.mapper.StrategyDetailMapper;
import cn.wolfcode.trip.base.mapper.StrategycatalogMapper;
import cn.wolfcode.trip.base.query.QueryObject;
import cn.wolfcode.trip.base.service.IStrategyDetailService;
import cn.wolfcode.trip.base.service.IStrategycatalogService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;


@Service
public class StrategyDetailServiceImpl implements IStrategyDetailService {
    @Autowired
    private StrategyDetailMapper strategyDetailMapper ;

    @Autowired
    private StrategyContentMapper strategyContentMapper ;

    public PageInfo<StrategyDetail> query(QueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize(),qo.getOrderBy());
        List<StrategyDetail> list = strategyDetailMapper.selectForList(qo);
        return new PageInfo<>(list);
    }

    @Override
    public void saveOrUpdate(StrategyDetail strategyDetail) {
        if(strategyDetail.getSequence()==null){
            //判断用户有没有传递序号过来
            //如果没有传递序号过来，我就从数据库中查出这个攻略分类中的子文章中最大的序列
            Integer sequence = strategyDetailMapper.
                            selectSequenceByCatalogId(strategyDetail.getCatalog().getId());
            if(sequence==null){
                //如果这个攻略分类中没有字文章的话，就将这个序号设置为0
                sequence = 0 ;
            }
            //不管有没有查出来，将弄好的序号加上1赋值给这个对象
            strategyDetail.setSequence(sequence+1);
        }
        //还要判断状态是否是发布还是草稿
        if (strategyDetail.getState()== StrategyDetail.STATE_RELEASE) {
            //如果是发布的话，就设置发布的时间
            strategyDetail.setReleaseTime(new Date());
        }
        //执行新增或者修改语句
        if (strategyDetail.getId()==null) {
            //新增操作的话就设置文章的创建时间
            strategyDetail.setCreateTime(new Date());
            strategyDetailMapper.insert(strategyDetail);
            //因为是主键关联，新增完完文章的数据之后还要新增主键关联一对一的文章内容的数据
            StrategyContent content = strategyDetail.getStrategyContent();
            //将文章的id给到内容对象的id
            content.setId(strategyDetail.getId());
            //新增文章的数据
            strategyContentMapper.insert(content);
        }else{
            strategyDetailMapper.updateByPrimaryKey(strategyDetail);
            //因为是主键关联，新增完完文章的数据之后还要新增主键关联一对一的文章内容的数据
            StrategyContent content = strategyDetail.getStrategyContent();
            //将文章的id给到内容对象的id
            content.setId(strategyDetail.getId());
            //修改文章的数据
            strategyContentMapper.updateByPrimaryKey(content);
        }
    }

    @Override
    public String getContentByDetailId(Long id) {
        return strategyContentMapper.selectByPrimaryKey(id).getContent();
    }

    @Override
    public StrategyDetail get(Long id) {
        //根据传送过来的文章id，找到指定的文章内容和攻略文章
        StrategyDetail detail = strategyDetailMapper.selectByPrimaryKey(id);
        //找文章
        if(detail!=null){
            StrategyContent content = strategyContentMapper.selectByPrimaryKey(detail.getId());
            //将文章的对象设置进去给攻略文章对象
            detail.setStrategyContent(content);
        }
        return detail;
    }
}
